package com.xz.stu.server.controller;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.xz.stu.server.service.ClassService;
import com.xz.stu.server.vo.stuClass.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 班级管理")
@RestController
@RequestMapping("/admin-api/school/class")
@Validated
public class ClassController {

    @Resource
    private ClassService classService;

    @PostMapping("/create")
    @Operation(summary = "创建班级")
    @PreAuthorize("@ss.hasPermission('school:class:add')")
    public CommonResult<Long> createClass(@Valid @RequestBody ClassCreateReqVO createReqVO) {
        return success(classService.createClass(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级")
    @PreAuthorize("@ss.hasPermission('school:class:edit')")
    public CommonResult<Boolean> updateClass(@Valid @RequestBody ClassUpdateReqVO updateReqVO) {
        classService.updateClass(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除班级")
    @Parameter(name = "id", description = "班级编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('school:class:remove')")
    public CommonResult<Boolean> deleteClass(@PathVariable("id") List<Long> id) {
        classService.deleteClass(id);
        return success(true);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "获得班级详情")
    @Parameter(name = "id", description = "班级编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('school:class:query')")
    public CommonResult<ClassRespVO> getClass(@PathVariable("id") Long id) {
        return success(classService.getStuClass(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级分页")
    @PreAuthorize("@ss.hasPermission('school:class:list')")
    public CommonResult<PageResult<ClassRespVO>> getClassPage(@Valid ClassPageReqVO pageVO) {
        return success(classService.getClassPage(pageVO));
    }

    @GetMapping("/list-by-dept")
    @Operation(summary = "获得指定院系的班级列表")
    @Parameter(name = "deptId", description = "院系编号", required = true, example = "1024")
    public CommonResult<List<ClassSimpleRespVO>> getClassListByDeptId(@RequestParam("deptId") Long deptId) {
        return success(classService.getClassListByDeptId(deptId));
    }
}
